/*
 * Copyright 2018-10-18 13:38:10 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.utils.Constants;
import com.lap.scp.admin.utils.ExcelHeader;
import com.lap.scp.admin.utils.ExcelUtils;
import com.lap.scp.erp.ao.PurchaseOrdersAO;
import com.lap.scp.erp.core.PurchaseOrdersManager;
import com.lap.scp.erp.domain.PurchaseOrdersDO;
import com.lap.scp.erp.query.PurchaseOrdersQueryVO;

/**
 * <pre>
 *   默认 记录物料出入库信息应用实现类，提供记录物料出入库信息相关的流程应用操作、查询等。
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-18 13:38:10
 * @since 2.0.0
 */
@Service("purchaseOrdersAO")
public class DefaultPurchaseOrdersAO extends AbstractAO implements PurchaseOrdersAO {
	/**
	 * 记录物料出入库信息业务操作对象
	 */
	@Resource
	private PurchaseOrdersManager purchaseOrdersManager;

	@Override
	public PageResult<PurchaseOrdersDO> queryPage(Page page, PurchaseOrdersQueryVO queryVO) {
		PageResult<PurchaseOrdersDO> pr = new PageResult<>();
		try {
			List<PurchaseOrdersDO> list = purchaseOrdersManager.queryPage(page, queryVO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(new Object[] { page, queryVO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Workbook> export(PurchaseOrdersQueryVO queryVO) {
		Resp<Workbook> resp = new Resp<>();
		Page page = new Page(1000);
		try {
			Workbook wb = new HSSFWorkbook();
			resp.setData(wb);

			ExcelUtils.writeHeader(wb, ExcelHeader.PURCHASE_HEADER, Constants.PURCHASE_ORDERS);
			int curPage = 1;
			int startRow = 0;
			do {
				List<PurchaseOrdersDO> list = purchaseOrdersManager.queryPage(page, queryVO);
				ExcelUtils.writeContent(wb, ExcelHeader.PURCHASE_EXCELTEYS, list, Constants.PURCHASE_ORDERS, startRow);

				startRow = startRow * list.size();
				curPage++;
			} while (curPage <= page.getTotalPage());
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(new Object[] { page, queryVO }, e);
		}
		return resp;
	}

}
